package com.goldcn.user.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goldcn.common.util.CommonUtils;
import com.goldcn.common.util.IdGen;
import com.goldcn.interceptor.SystemContext;
import com.goldcn.user.dao.MessageMapper;
import com.goldcn.user.dao.UserInfoMapper;
import com.goldcn.user.dao.UserMessageMapper;
import com.goldcn.user.model.Message;
import com.goldcn.user.model.UserInfo;
import com.goldcn.user.model.UserMessage;
import com.google.common.base.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 1111 on 2016/12/6.
 */
@Service
@Transactional
public class MessageService {
    @Autowired
    MessageMapper messageMapper;

    @Autowired
    UserMessageMapper userMessageMapper;

    @Autowired
    UserInfoMapper userInfoMapper;

    public Object addMessage(Message message){
        message.setId(IdGen.get().nextId());
        message.setAddTime(CommonUtils.getCurTimestamp());
        message.setCompanyID(SystemContext.getCompanyID());
        messageMapper.insertSelective(message);

       /* List<UserInfo> users = Objects.equal(message.getType(),1) ? userInfoMapper.selAdminUser(SystemContext.getCompanyID()) : userInfoMapper.selAppUser(SystemContext.getCompanyID());
        List<UserMessage> list = Lists.newArrayList();
        for(UserInfo userInfo : users){
            UserMessage userMessage = new UserMessage(userInfo.getUserID(),message.getType(),message.getMessage(),0,message.getAddTime());
            userMessage.setId(IdGen.get().nextId());
            list.add(userMessage);
        }
        userMessageMapper.insertBatch(list);*/
        return "success";
    }

    public PageInfo selectAllMessage(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Message> messageList = messageMapper.selectMessageByType(null,SystemContext.getCompanyID());
        PageInfo<Message> page = new PageInfo(messageList);
        return page;
    }

    //type 1是pc端信息，2是APP端信息
    public PageInfo selectMessageByType(int type,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Message> messageList = messageMapper.selectMessageByType(String.valueOf(type),SystemContext.getCompanyID());
        PageInfo<Message> page = new PageInfo(messageList);
        return page;
    }

    //查询用户信息表信息
    public PageInfo selectUserMessageByUserID(Long userID,int pageNum,int pageSize){
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userID);
        String condition = new StringBuffer().append(" AND `type` != 1").toString();
        if(!Objects.equal(userInfo.getType(),2)){//不是员工
            condition = null;
        }else if(Objects.equal(userInfo.getIsAdmin(),1)){
            condition = null;
        }
        PageHelper.startPage(pageNum,pageSize);
        List<UserMessage> userMessageList = userMessageMapper.selectByUserID(userID,condition);
        PageInfo<UserMessage> page = new PageInfo(userMessageList);
        return page;
    }

    //查询用户信息表信息
    public PageInfo selectEmployMsgForWeb(Long userID,int pageNum,int pageSize){
        String condition = new StringBuffer().append(" AND `type` = 1").toString();
        PageHelper.startPage(pageNum,pageSize);
        List<UserMessage> userMessageList = userMessageMapper.selectByUserID(userID,condition);
        PageInfo<UserMessage> page = new PageInfo(userMessageList);
        return page;
    }

    //按id查询信息详情
    public UserMessage selectUserMessageByID(Long id){
        return userMessageMapper.selectByPrimaryKey(id);
    }

    //按id删除用户信息
    public String deleteUserMessageByID(Long id){
        userMessageMapper.deleteByPrimaryKey(id);
        return "success";
    }

    //修改信息状态为已读
    public Object updateStateByID(Long id){
        userMessageMapper.updateState(id);
        return "success";
    }

    //用户定时读取消息总表，存入消息用户表，并返回未读消息条数
    public Object getNotReadMessageQty(Long userID,boolean isWeb){
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userID);
        Map<String,Object> param = new HashMap<>();
        List<Integer> typeList = new ArrayList<>();
        //add buyadong 2017-6-13 09:20:37 查询不到数据返回0
        if(userInfo==null){
            return 0;
        }
        if(userInfo.getType()==2){
            typeList.add(1);
            typeList.add(2);
            typeList.add(3);
            param.put("typeList",typeList);
        }
        if(userInfo.getType()==3 || userInfo.getType()==4){//销售顾问和会员
            typeList.add(2);
            typeList.add(3);
            param.put("typeList",typeList);
        }else{
            typeList.add(1);
            param.put("typeList",typeList);
        }

        Long timesamp = userMessageMapper.selectMaxTimeByUserID(userID);
        if(timesamp==null){
            timesamp = userInfo.getAddTime();
        }
        param.put("addTime",timesamp);
        param.put("companyID", SystemContext.getCompanyID());
        List<Message> messageList = messageMapper.selectByTypeAndTime(param);
        if(messageList.size()>0) {
            List<UserMessage> userMessageList = new ArrayList<>();
            for (Message message : messageList) {
                UserMessage userMessage = new UserMessage();
                userMessage.setId(IdGen.get().nextId());
                userMessage.setAddTime(message.getAddTime());
                userMessage.setState(0);
                userMessage.setUserID(userID);
                userMessage.setType(message.getType());
                userMessage.setMessage(message.getMessage());
                userMessageList.add(userMessage);
            }
            userMessageMapper.insertBatch(userMessageList);
        }

        String condition = null;
        if(Objects.equal(userInfo.getType(),2)){//员工
            if(isWeb){
                condition = new StringBuffer().append(" AND `type` = 1").toString();
            }else{
                condition = new StringBuffer().append(" AND `type` != 1").toString();
            }
        }
        return userMessageMapper.selectNotReadQty(userID,condition);
    }
}
